// pages/gwc/gwc.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: -1,
    list: [],
    chebok: false,
    pricee: "0",
    users: wx.getStorageSync('addres') || {},
  },
  getaddres() {
    let token = wx.getStorageSync('token')
    if (token != "") {
      let addres = wx.getStorageSync("addres")
      this.setData({
        users: addres
      })
    }

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.geto()
    this.getprice()
    this.getaddres()

  },
  box() {
    this.setData({
      active: -1
    })
  },
  //滑动
  oned(e) {
    this.setData({
      active: e.currentTarget.dataset.index
    })
  },

  geto() {
    let arr = wx.getStorageSync('list') || []
    let arr2 = arr.filter(item => {
      return item.checked == true
    })
    if (arr.length == arr2.length) {
      this.setData({
        chebok: true
      })
    } else {
      this.setData({
        chebok: false
      })
    }
    if (arr.length == 0) {
      this.setData({
        chebok: false
      })
    }
    this.getprice()

  },
  del() {
    let arr = wx.getStorageSync('list') || []
    arr.splice(this.data.active, 1)
    wx.setStorageSync('list', arr)
    this.getlist()
    this.setData({
      active: -1
    })
    this.getprice()
  },
  jia(e) {
    let index = e.currentTarget.dataset.index
    let arr = wx.getStorageSync('list') || []
    arr[index].num++
    wx.setStorageSync('list', arr)
    this.getlist()
    this.getprice()

  },
  jian(e) {
    let index = e.currentTarget.dataset.index
    let arr = wx.getStorageSync('list') || []
    if (arr[index].num > 1) {
      arr[index].num--
      wx.setStorageSync('list', arr)
      this.getlist()
      this.getprice()
    } else {
      wx.showToast({
        title: '最低一个商品哦！~',
        icon: "none"
      })
    }

  },
  add() {
    let token = wx.getStorageSync('token')
    let users = wx.getStorageSync('addres')
    if (token == "") {
      wx.showModal({
        cancelColor: 'cancelColor',
        title: "您还未登录，是否去登录？",
        confirmText: "是",
        cancelText: "否",
        cancelColor: "#000",
        success: (res => {
          if (res.confirm) {
            wx.switchTab({
              url: '../gr/gr',
            })
            wx.setStorageSync('long', "ok")
          }
          if (res.cancel) {
            wx.showToast({
              title: '已取消',
              icon: "none",
            })
          }
        })
      })

    } else {
      if (users.name == null) {
        wx.showModal({
          cancelColor: 'cancelColor',
          title: "您还未选择收货地址！是否选择",
          confirmText: "是",
          cancelText: "否",
          cancelColor: "#000",
          success: (res => {
            console.log(res);
            if (res.confirm) {
              wx.getSetting({
                success: (res => {
                  if (res.authSetting["scope.address"] == true) {
                    wx.chooseAddress({
                      success: (addres) => {
                        addres.all = addres.provinceName + addres.cityName + addres.countyName + addres.detailInfo //地址
                        addres.name = addres.userName //用户名
                        addres.tel = addres.telNumber
                        let obj = {
                          dizhi: addres.all,
                          name: addres.name,
                          telnum: addres.tel
                        }
                        console.log(obj);
                        wx.setStorageSync('addres', obj)
                        wx.switchTab({
                          url: '../gwc/gwc'
                        })
                      },
                      fail: (err => {
                        console.log("用户地址失败", err);
                      })
                    })
                  }
                })
              })
            }
            if (res.cancel) {
              wx.showToast({
                title: '取消退出',
                icon: "none",
              })
            }
          })
        })
        return false
      }
      // 
      let arr = wx.getStorageSync('list') || []
      let arr2 = arr.filter(item => {
        return item.checked == true
      })
      if (arr2.length >= 1) {
        wx.navigateTo({
          url: '../din/din',
        })
      } else {
        wx.showToast({
          title: '！至少得选择一件商品',
          icon: "none"
        })
      }
    }


  },
  //反选
  checkboxAll(e) {
    let index = e.currentTarget.dataset.index
    let arr = wx.getStorageSync('list') || []
    arr[index].checked = !arr[index].checked
    wx.setStorageSync('list', arr)
    //数据更新
    this.getlist()
    //判断全选按钮
    this.geto()
    //计算价钱的
    this.getprice()


  },
  //全选

  quan() {
    let token = wx.getStorageSync('token')
    if (token != "") {
      this.getprice()
      let arr = wx.getStorageSync('list') || []
      arr.forEach(item => {
        item.checked = !this.data.chebok
      })
      wx.setStorageSync('list', arr)
      this.getlist()
      this.geto()
    } else {
      wx.showToast({
        title: '您还未登录',
        icon: 'none'
      })
    }



  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  getlist() {
    let token = wx.getStorageSync('token')
    if (token != "") {
      this.setData({
        list: wx.getStorageSync('list') || []
      })
    }
  },
  getprice() {
    let token = wx.getStorageSync('token')
    if (token != "") {
      let arr = wx.getStorageSync('list') || []
      let prac = 0
      arr.forEach(item => {
        if (item.checked == true) {
          prac += item.num * item.price
        }
      })
      this.setData({
        pricee: prac
      })
    }

  },
  //获取地址信息
  dizhi() {
    let token = wx.getStorageSync('token')

    if (token == "") {
      wx.showModal({
        cancelColor: 'cancelColor',
        title: "您还未登录是否去登录？",
        confirmText: "是",
        cancelText: "否",
        cancelColor: "#000",
        success: (res => {
          if (res.confirm) {
            wx.switchTab({
              url: '../gr/gr',
            })
            wx.setStorageSync('long', "ok")
          }
          if (res.cancel) {
            wx.showToast({
              title: '取消成功',
              icon: "none"
            })
          }
        })
      })
    } else {
      wx.getSetting({
        success: (res => {
          if (res.authSetting["scope.address"] == true) {
            wx.chooseAddress({ 
              success: (addres) => {
                addres.all = addres.provinceName + addres.cityName + addres.countyName + addres.detailInfo //地址
                addres.name = addres.userName //用户名
                addres.tel = addres.telNumber
                let obj = {
                  dizhi: addres.all,
                  name: addres.name,
                  telnum: addres.tel
                }
                wx.setStorageSync('addres', obj)
              },
              fail: (err => {
              })
            })
          }
        })
      })
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    this.getlist()
    this.geto()
    this.getprice()
    this.getaddres()


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})